package com.jincp.common.networkstate;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 检测当前环境的网络状态
 * Author : zhousf
 * 静态注册：
 * <receiver android:name="core.networkstate.NetworkStateReceiver" >
 * <intent-filter>
 * <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
 * <action android:name="android.gzcpc.conn.CONNECTIVITY_CHANGE" />
 * </intent-filter>
 * </receiver>
 * 动态注册：
 * registerNetworkStateReceive/unRegisterNetworkStateReceiver
 * 权限：
 * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 * <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
 * <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
 */
public class NetworkStateReceiver extends BroadcastReceiver {

    private static BroadcastReceiver broadcastReceiver;
    private static ArrayList<NetworkStateListener> networkStateListenerList = new ArrayList<>();
    private final static String ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";


    public static BroadcastReceiver getBroadcastReceiver() {
        return broadcastReceiver == null ? broadcastReceiver = new NetworkStateReceiver() : broadcastReceiver;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (broadcastReceiver == null)
            broadcastReceiver = NetworkStateReceiver.this;
        if (intent.getAction().equalsIgnoreCase(ANDROID_NET_CHANGE_ACTION)) {
            boolean isNetworkAvailable = NetworkUtil.isNetworkAvailable(context);
            String networkType = NetworkUtil.getNetworkType(context);
            NetInfo netInfo = new NetInfo(isNetworkAvailable, networkType);
            notifyListener(isNetworkAvailable, netInfo);
        }
    }


    /**
     * 通知网络状态监听器
     */
    private void notifyListener(boolean isNetworkAvailable, NetInfo netInfo) {
        for (int i = 0; i < networkStateListenerList.size(); i++) {
            final NetworkStateListener listener = networkStateListenerList.get(i);
            if (null != listener) {
                listener.onNetworkState(isNetworkAvailable, netInfo);
            }
        }
    }

    /**
     * 添加网络状态监听
     */
    public static void addNetworkStateListener(NetworkStateListener observer) {
        if (null == networkStateListenerList) {
            networkStateListenerList = new ArrayList<>();
        }
        networkStateListenerList.add(observer);
    }

    /**
     * 移除网络状态监听
     */
    public static void removeNetworkStateListener(NetworkStateListener observer) {
        if (null != networkStateListenerList) {
            networkStateListenerList.remove(observer);
        }
    }

    /**
     * 注册网络状态广播
     */
    public static void registerNetworkStateReceiver(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ANDROID_NET_CHANGE_ACTION);
        LocalBroadcastManager.getInstance(context).registerReceiver(getBroadcastReceiver(), filter);
    }


    /**
     * 注销网络状态广播
     */
    public static void unRegisterNetworkStateReceiver(Context context) {
        if (null != getBroadcastReceiver()) {
            try {
                LocalBroadcastManager.getInstance(context).unregisterReceiver(getBroadcastReceiver());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
